import * as z from 'zod';
import type { FormSchema } from '../types';

// 状态选项
const statusOptions = [
  { label: '启用', value: 1 },
  { label: '禁用', value: 0 },
];

// 字典项目表单 Schema
export const dictionaryItemFormSchema: FormSchema = {
  config: {
    fields: [
      {
        name: 'code',
        type: 'text',
        label: '编码',
        placeholder: '请输入项目编码',
        required: true,
        grid: { span: 1 },
        helpText: '字典项目的唯一标识编码',
      },
      {
        name: 'label',
        type: 'text',
        label: '标签',
        placeholder: '请输入显示标签',
        required: true,
        grid: { span: 1 },
      },
      {
        name: 'value',
        type: 'text',
        label: '值',
        placeholder: '请输入项目值',
        required: true,
        grid: { span: 1 },
      },
      {
        name: 'status',
        type: 'select',
        label: '状态',
        placeholder: '请选择状态',
        options: statusOptions,
        grid: { span: 1 },
      },
      {
        name: 'sort',
        type: 'number',
        label: '排序',
        placeholder: '请输入排序值',
        grid: { span: 1 },
        helpText: '数字越小排序越靠前',
      },
    ],
    layout: {
      columns: 2,
      spacing: 'space-y-4',
    },
    submitButton: {
      text: '保存',
    },
    cancelButton: {
      text: '取消',
      show: true,
    },
  },
  validationSchema: z.object({
    code: z.string().min(1, '编码不能为空').max(50, '编码最多50个字符'),
    label: z.string().min(1, '标签不能为空').max(100, '标签最多100个字符'),
    value: z.string().min(1, '值不能为空').max(100, '值最多100个字符'),
    status: z.number(),
    sort: z.number(),
  }),
  initialValues: {
    code: '',
    label: '',
    value: '',
    status: 1,
    sort: 0,
  },
};

// 创建字典项目表单 Schema
export const createDictionaryItemFormSchema: FormSchema = {
  ...dictionaryItemFormSchema,
  validationSchema: z.object({
    dictionaryId: z.string().min(1, '字典ID不能为空'),
    code: z.string().min(1, '编码不能为空').max(50, '编码最多50个字符'),
    label: z.string().min(1, '标签不能为空').max(100, '标签最多100个字符'),
    value: z.string().min(1, '值不能为空').max(100, '值最多100个字符'),
    status: z.number(),
    sort: z.number(),
    parentId: z.string().optional(),
  }),
  initialValues: {
    dictionaryId: '',
    code: '',
    label: '',
    value: '',
    status: 1,
    sort: 0,
    parentId: undefined,
  },
};

// 编辑字典项目表单 Schema
export const editDictionaryItemFormSchema: FormSchema = {
  ...dictionaryItemFormSchema,
  validationSchema: z.object({
    id: z.string().min(1, '项目ID不能为空'),
    code: z.string().min(1, '编码不能为空').max(50, '编码最多50个字符'),
    label: z.string().min(1, '标签不能为空').max(100, '标签最多100个字符'),
    value: z.string().min(1, '值不能为空').max(100, '值最多100个字符'),
    status: z.number(),
    sort: z.number(),
  }),
};
